package edu.ua.Team_Industry;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;


public class myMapOverlay extends com.google.android.maps.ItemizedOverlay {
	
	public ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	public OverlayItem overlayItem;
	//public boolean firstTouch;
	public Industry_Map indMap;
	public int mIndex;
	public Context mcontext;
	Drawable redDot;
	Drawable purpleDot;
	Drawable blueDot;
	Drawable greenDot;
	Drawable yellowDot;
	
	public GeoPoint newPoint;
	public String url;
	
	public String[] tagType;
	
	

	public myMapOverlay(Drawable defaultMarker, Industry_Map iMap) {
		super(boundCenterBottom(defaultMarker));
		mcontext = iMap;
		overlayItem = null;
		//firstTouch = true;
		this.indMap = iMap;
		
		tagType = new String[6];
		
		tagType[1] = "Emergency";
		tagType[2] = "Aid";
		tagType[3] = "Obstruction";
		tagType[4] = "Damage";
		tagType[5] = "Other";
		
		redDot = mcontext.getResources().getDrawable(R.drawable.dot);
		redDot.setBounds(0, 0, 14, 14);
		blueDot = mcontext.getResources().getDrawable(R.drawable.dot_blue);
		blueDot.setBounds(0, 0, 14, 14);
        greenDot = mcontext.getResources().getDrawable(R.drawable.dot_green);
        greenDot.setBounds(0, 0, 14, 14);
        yellowDot = mcontext.getResources().getDrawable(R.drawable.dot_yellow);
        yellowDot.setBounds(0, 0, 14, 14);
		purpleDot = mcontext.getResources().getDrawable(R.drawable.dot_purple);
		purpleDot.setBounds(0, 0, 14, 14);
		
		populate(); 
		// TODO Auto-generated constructor stub
	}
	public void addOverlay(OverlayItem overlay) {
		if (Integer.valueOf(overlay.getTitle())==1){
			overlay.setMarker(redDot);
		}
		if (Integer.valueOf(overlay.getTitle())==2){
			overlay.setMarker(blueDot);
		}
		if (Integer.valueOf(overlay.getTitle())==3){
			overlay.setMarker(greenDot);
		}
		if (Integer.valueOf(overlay.getTitle())==4){
			overlay.setMarker(yellowDot);
		}
		if (Integer.valueOf(overlay.getTitle())==5){
			overlay.setMarker(purpleDot);
		}
	    mOverlays.add(overlay);
	    populate();
	}
	
	public void removeTap(){
		populate();
	}
	
	public void removeAll(){
		mOverlays.clear();
	}

	@Override
	protected OverlayItem createItem(int i) {
	  return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}
	
	@Override
	public boolean onTap(GeoPoint point, MapView mapView) {
		boolean tapped = super.onTap(point, mapView);
		if (tapped) 
		{
			overlayItem = mOverlays.get(mIndex);
			AlertDialog.Builder dialog = new AlertDialog.Builder(mcontext);
			dialog.setTitle(tagType[Integer.valueOf(overlayItem.getTitle())]);
			dialog.setMessage(overlayItem.getSnippet());
			dialog.setPositiveButton("Exit", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					
				}
			});
			dialog.show();
			return true;
		}
		else 
		{
			try {
				int lat = (int) point.getLatitudeE6();
				int lng = (int) point.getLongitudeE6();
				this.newPoint = new GeoPoint(lat, lng);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			indMap.openContextMenu(mapView);
		}
		return true;
	}
	
	@Override
	public boolean onTap(int index) {
		mIndex = index;
		return true;
	}

}
